package com.ahstu.learning.notepad;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ahstu.learning.R;

public class updataActivity extends Activity {
    int id;
    EditText edit1;
    EditText edit2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.updatenote);
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        edit1 = (EditText) findViewById(R.id.note_text1_update);
        edit2 = (EditText) findViewById(R.id.note_text2_update);
        fillData();
        Button updataButton = (Button) findViewById(R.id.btn_note_protect_update);
        Button updataCancelButton = (Button) findViewById(R.id.btn_note_cancel_update);
        Button deleteButton = (Button) findViewById(R.id.btn_note_delete);

        updataButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Database help = new Database(updataActivity.this, "node.db", null, 1);
                SQLiteDatabase db = help.getWritableDatabase();
                ContentValues value = new ContentValues();
                String title = edit1.getText().toString();
                if (title.equals("")) {
                    Toast.makeText(updataActivity.this, "请输入标题", Toast.LENGTH_SHORT).show();

                    return;

                }
                String content = edit2.getText().toString();
                value.put("title", title);
                value.put("content", content);
                db.update("thing", value, "id=?", new String[]{id + ""});
                Intent intent = new Intent(updataActivity.this, ListActivity.class);
                ListActivity.list.finish();
                finish();
                db.close();
                startActivity(intent);


            }
        });

        updataCancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        deleteButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                Database help = new Database(updataActivity.this, "node.db", null, 1);
                SQLiteDatabase db = help.getWritableDatabase();
                db.delete("thing", "id=?", new String[]{id + ""});

                db.close();

                Intent intent = new Intent(updataActivity.this, ListActivity.class);
                ListActivity.list.finish();
                finish();

                startActivity(intent);


            }
        });


    }

    private void fillData() {
        Database helper = new Database(updataActivity.this, "node.db", null, 1);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cs = db.query("thing", new String[]{"id", "title", "content"}, "id=?", new String[]{id + ""}, null, null, null);
        while (cs.moveToNext()) {

            String title = cs.getString(cs.getColumnIndex("title"));
            String content = cs.getString(cs.getColumnIndex("content"));
            edit1.setText(title);
            edit2.setText(content);


        }

        db.close();


    }
}
